package com.team2.cyber_eye.ActivityHelpers;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.team2.cyber_eye.R;
import com.team2.shared.Store;

public class AdapterStore extends BaseAdapter {
    
    private Activity activity;
    private ArrayList<Store> stores;
    private static LayoutInflater inflater=null;
    
    public AdapterStore(Activity a, ArrayList<Store> s)
    {
        activity = a;
        if (s==null)
        	s = new ArrayList<Store>();
        stores = s;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
    	if(stores.size()==0) return 1;
        return stores.size();
    }

    public Object getItem(int position) {
    	if (position < 0 || position >= stores.size())
    		return null;
    	
        return stores.get(position);
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item_store, null);
        if(stores.isEmpty()) {
        	TextView name=(TextView)vi.findViewById(R.id.name);
        	name.setText("No Stores Found");
        	ImageView image=(ImageView)vi.findViewById(R.id.image);
        	image.setImageDrawable(vi.getResources().getDrawable(R.drawable.no_image));
        }
        else
        {
            TextView name=(TextView)vi.findViewById(R.id.name);
            ImageView image=(ImageView)vi.findViewById(R.id.image);
            Store str = stores.get(position);
            name.setText(str.getAddress());
            
            String storeName = str.getName().toLowerCase();
            
            if(storeName.contains("best") || storeName.contains("buy")) {
            	image.setImageDrawable(vi.getResources().getDrawable(R.drawable.bestbuy)); 
            } else if(storeName.contains("future")) {
            	image.setImageDrawable(vi.getResources().getDrawable(R.drawable.futureshop));
            } else if(storeName.contains("chapters")) {
            	image.setImageDrawable(vi.getResources().getDrawable(R.drawable.chapters));
            } else if(storeName.contains("sears")) {
            	image.setImageDrawable(vi.getResources().getDrawable(R.drawable.sears));
            } else if(storeName.contains("bay")) {
            	 image.setImageDrawable(vi.getResources().getDrawable(R.drawable.thebay));
            } else if(storeName.contains("walmart")) {
            	image.setImageDrawable(vi.getResources().getDrawable(R.drawable.walmart));
            } else if(storeName.contains("london")) {
            	image.setImageDrawable(vi.getResources().getDrawable(R.drawable.londondrugs));
            } else {
            	image.setImageDrawable(vi.getResources().getDrawable(R.drawable.no_image));
            }
        }
        return vi;
    }
}
